#include "headfiles.h"

using namespace std;

MoveDisk::MoveDisk(int disk_num) {
	disk_num_ = disk_num;
	// Allocate three stacks
	stacks_[0] = new MyStack<int>();
	stacks_[1] = new MyStack<int>();
	stacks_[2] = new MyStack<int>();
	if (stacks_[0] == NULL ||
		stacks_[1] == NULL ||
		stacks_[2] == NULL) {
		cout << "Allocate stack error!" << endl;
		abort();
	}
	
	// Put disks in stack 1
	int i;
	for (i = 0; i < disk_num_; i++) {
		stacks_[0]->push(disk_num_ - i);
	}
}

MoveDisk::~MoveDisk() {
	stacks_[0]->clear();
	stacks_[1]->clear();
	stacks_[2]->clear();
}

bool MoveDisk::moveTop(int src, int dest) {
	if (stacks_[src]->size() > 0) {
		int val = stacks_[src]->top();
		cout << "Move disk " << val << " from stack[" << src << "] to stack[" << dest << "]." << endl;
		stacks_[src]->pop();
		stacks_[dest]->push(val);
		return true;
	}
	else {
		cout << "Stack[" << src << "] is empty!" << endl;
		return false;
	}
}

bool MoveDisk::move(int src, 
					int dest, 
					int bridge, 
					int disk_num) {
	if (disk_num > 0) {
//		stacks_[0]->print();
//		stacks_[1]->print();
//		stacks_[2]->print();
		move(src, bridge, dest, disk_num - 1);
		moveTop(src, dest);
		move(bridge, dest, src, disk_num - 1);
	}

	return true;
}

bool MoveDisk::move() {
	return move(0, 2, 1, disk_num_);
}


